﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:NMBP.View.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:NMBP.View.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

namespace NMBP.View.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:NMBP.View.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:NMBP.View.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Keeps reference to main view model
        /// </summary>
        private static MainViewModel main;

        /// <summary>
        /// Keeps reference to cart purchase view model
        /// </summary>
        private static CartPurchaseViewModel cartPurchaseViewModel;

        /// <summary>
        /// Keeps reference to customer view model
        /// </summary>
        private static CustomerViewModel customerViewModel;

        /// <summary>
        /// Keeps reference to edit customer view model
        /// </summary>
        private static EditCustomerViewModel editCustomerViewModel;

        /// <summary>
        /// Keeps reference to edit worker view model
        /// </summary>
        private static EditWorkerViewModel editWorkerViewModel;

        /// <summary>
        /// Keeps reference to worker view model
        /// </summary>
        private static WorkerViewModel workerViewModel;

        /// <summary>
        /// Keeps reference to admin view model
        /// </summary>
        private static AdminViewModel adminViewModel;

        /// <summary>
        /// Keeps reference to edit customer from admin view model
        /// </summary>
        private static EditCustomerFromAdminViewModel editCustomerFromAdminViewModel;

        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}

            CreateMain();
        }

        #region Static properties
        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (main == null)
                {
                    CreateMain();
                }

                return main;
            }
        }

        /// <summary>
        /// Gets the CartPurchaseViewModel property.
        /// </summary>
        public static CartPurchaseViewModel CartPurchaseViewModelStatic
        {
            get
            {
                if (cartPurchaseViewModel == null)
                {
                    CreateCartPurchaseViewModel();
                }

                return cartPurchaseViewModel;
            }
        }

        /// <summary>
        /// Gets the CustomerViewModel property.
        /// </summary>
        public static CustomerViewModel CustomerViewModelStatic
        {
            get
            {
                if (customerViewModel == null)
                {
                    CreateCustomerViewModel();
                }

                return customerViewModel;
            }
        }

        /// <summary>
        /// Gets the EditCustomerViewModel property.
        /// </summary>
        public static EditCustomerViewModel EditCustomerViewModelStatic
        {
            get
            {
                if (editCustomerViewModel == null)
                {
                    CreateEditCustomerViewModel();
                }

                return editCustomerViewModel;
            }
        }

        /// <summary>
        /// Gets the EditWorkerViewModel property.
        /// </summary>
        public static EditWorkerViewModel EditWorkerViewModelStatic
        {
            get
            {
                if (editWorkerViewModel == null)
                {
                    CreateEditWorkerViewModel();
                }

                return editWorkerViewModel;
            }
        }

        /// <summary>
        /// Gets the WorkerViewModel property.
        /// </summary>
        public static WorkerViewModel WorkerViewModelStatic
        {
            get
            {
                if (workerViewModel == null)
                {
                    CreateWorkerViewModel();
                }

                return workerViewModel;
            }
        }

        /// <summary>
        /// Gets the AdminViewModel property.
        /// </summary>
        public static AdminViewModel AdminViewModelStatic
        {
            get
            {
                if (adminViewModel == null)
                {
                    CreateAdminViewModel();
                }

                return adminViewModel;
            }
        }

        /// <summary>
        /// Gets the EditCustomerFromAdminViewModel property.
        /// </summary>
        public static EditCustomerFromAdminViewModel EditCustomerFromAdminViewModelStatic
        {
            get
            {
                if (editCustomerFromAdminViewModel == null)
                {
                    CreateEditCustomerFromAdminViewModel();
                }

                return editCustomerFromAdminViewModel;
            }
        }

        /// <summary>
        /// Gets the EditCustomerFromAdminViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public EditCustomerFromAdminViewModel EditCustomerFromAdminViewModel
        {
            get
            {
                return EditCustomerFromAdminViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Gets the CartPurchaseViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public CartPurchaseViewModel CartPurchaseViewModel
        {
            get
            {
                return CartPurchaseViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the CustomerViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public CustomerViewModel CustomerViewModel
        {
            get
            {
                return CustomerViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the EditCustomerViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public EditCustomerViewModel EditCustomerViewModel
        {
            get
            {
                return EditCustomerViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the EditWorkerViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public EditWorkerViewModel EditWorkerViewModel
        {
            get
            {
                return EditWorkerViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the WorkerViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public WorkerViewModel WorkerViewModel
        {
            get
            {
                return WorkerViewModelStatic;
            }
        }

        /// <summary>
        /// Gets the AdminViewModel property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AdminViewModel AdminViewModel
        {
            get
            {
                return AdminViewModelStatic;
            }
        }
        #endregion

        #region Create clear methods
        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            main.Cleanup();
            main = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (main == null)
            {
                main = new MainViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the CartPurchaseViewModel property.
        /// </summary>
        public static void ClearCartPurchaseViewModel()
        {
            cartPurchaseViewModel.Cleanup();
            cartPurchaseViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the CartPurchaseViewModel property.
        /// </summary>
        public static void CreateCartPurchaseViewModel()
        {
            if (cartPurchaseViewModel == null)
            {
                cartPurchaseViewModel = new CartPurchaseViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the CustomerViewModel property.
        /// </summary>
        public static void ClearCustomerViewModel()
        {
            customerViewModel.Cleanup();
            customerViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the CustomerViewModel property.
        /// </summary>
        public static void CreateCustomerViewModel()
        {
            if (customerViewModel == null)
            {
                customerViewModel = new CustomerViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the EditCustomerViewModel property.
        /// </summary>
        public static void ClearEditCustomerViewModel()
        {
            editCustomerViewModel.Cleanup();
            editCustomerViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the EditCustomerViewModel property.
        /// </summary>
        public static void CreateEditCustomerViewModel()
        {
            if (editCustomerViewModel == null)
            {
                editCustomerViewModel = new EditCustomerViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the EditWorkerViewModel property.
        /// </summary>
        public static void ClearEditWorkerViewModel()
        {
            editWorkerViewModel.Cleanup();
            editWorkerViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the EditWorkerViewModel property.
        /// </summary>
        public static void CreateEditWorkerViewModel()
        {
            if (editWorkerViewModel == null)
            {
                editWorkerViewModel = new EditWorkerViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the WorkerViewModel property.
        /// </summary>
        public static void ClearWorkerViewModel()
        {
            workerViewModel.Cleanup();
            workerViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the WorkerViewModel property.
        /// </summary>
        public static void CreateWorkerViewModel()
        {
            if (workerViewModel == null)
            {
                workerViewModel = new WorkerViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the AdminViewModel property.
        /// </summary>
        public static void ClearAdminViewModel()
        {
            adminViewModel.Cleanup();
            adminViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the AdminViewModel property.
        /// </summary>
        public static void CreateAdminViewModel()
        {
            if (adminViewModel == null)
            {
                adminViewModel = new AdminViewModel();
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the EditCustomerFromAdminViewModel property.
        /// </summary>
        public static void ClearEditCustomerFromAdminViewModel()
        {
            editCustomerFromAdminViewModel.Cleanup();
            editCustomerFromAdminViewModel = null;
        }

        /// <summary>
        /// Provides a deterministic way to create the EditCustomerFromAdminViewModel property.
        /// </summary>
        public static void CreateEditCustomerFromAdminViewModel()
        {
            if (editCustomerFromAdminViewModel == null)
            {
                editCustomerFromAdminViewModel = new EditCustomerFromAdminViewModel();
            }
        }
        #endregion
    }
}